/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author VuTienIT
 */
public class HinhCTPhieuDAO {
    
    public static ArrayList<HinhCTPhieuPOJO> getDanhSach()
    {
        ArrayList<HinhCTPhieuPOJO> s = new ArrayList<HinhCTPhieuPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From HinhCTPhieu";
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                HinhCTPhieuPOJO temp = new HinhCTPhieuPOJO();
                temp.setMaHinh(rs.getInt("MaHinh"));
                temp.setTenHinh(rs.getString("TenHinh"));
                temp.setMoTa(rs.getString("MoTa"));
                temp.setMaPhieu(rs.getInt("MaPhieu"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(HinhCTPhieuPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    public static ArrayList<HinhCTPhieuPOJO> getDanhSach(int maPhieu)
    {
        ArrayList<HinhCTPhieuPOJO> s = new ArrayList<HinhCTPhieuPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From HinhCTPhieu Where MaPhieu="+maPhieu;
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                HinhCTPhieuPOJO temp = new HinhCTPhieuPOJO();
                temp.setMaHinh(rs.getInt("MaHinh"));
                temp.setTenHinh(rs.getString("TenHinh"));
                temp.setMoTa(rs.getString("MoTa"));
                temp.setMaPhieu(rs.getInt("MaPhieu"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(HinhCTPhieuDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    public static ArrayList<HinhCTPhieuPOJO> getDanhSachCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<HinhCTPhieuPOJO> ds = new ArrayList<HinhCTPhieuPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_HINHCTPHIEU_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            HinhCTPhieuPOJO temp = new HinhCTPhieuPOJO();
            temp.setMaHinh(rs.getInt("MaHinh"));
            temp.setTenHinh(rs.getString("TenHinh"));
            temp.setMoTa(rs.getString("MoTa"));
            temp.setMaPhieu(rs.getInt("MaPhieu"));                  
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
    
     public static HinhCTPhieuPOJO getById(int id){
       HinhCTPhieuPOJO obj=new HinhCTPhieuPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from hinhctphieu where mahinh="+id);
       try {
           while (rs.next()) {            
            obj.setMaHinh(rs.getInt("MaHinh"));
            obj.setTenHinh(rs.getString("TenHinh"));
            obj.setMoTa(rs.getString("MoTa"));
            obj.setMaPhieu(rs.getInt("MaPhieu"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
}
